//
//  MYBasicNavViewController.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/20.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit

class MYBasicNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置UIGestureRecognizerDelegate代理
        self.interactivePopGestureRecognizer?.delegate = self

    }
    
    //重写push方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        var backTitle = "返回"
        
        if childViewControllers.count > 0 {
            
            //第一push层返回title用导航条的title,其余的用"返回"
            if childViewControllers.count == 1 {
                
                backTitle = childViewControllers.first?.title ?? "返回"
            }
            
            let backItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(backToFontVc))
            
            viewController.navigationItem.leftBarButtonItem = backItem
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    
    @objc private func backToFontVc() {
        popViewControllerAnimated(true)
    }
    
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if childViewControllers.count > 1 {
            return true
        }
        return false
    }



}
